<?php
$result = [
    'status' => 0,
    'data' => null,
    'message' => '未知错误'
];

$myhost = 'localhost:3306';
$myuser = 'root';
$mypass = '';

// 数据库链接
$mysql = new mysqli($myhost, $myuser, $mypass);

// 检测连接
if ($mysql->connect_error) {
    $sql = false;
    
    // die("连接数据库失败! " . $mysql->connect_error);
}
else {
    $sql = true;

    // 设置编码方式
    $mysql->query('set names utf8');
};

include_once 'sql-fun.php';

if (isset($sql) && $sql) {
    $params = [];
    $params['is'] = isset($_GET['is']) && ($_GET['is'] < 0) ? -1 : 1;
    $params['sqlname'] = isset($_GET['name']) ? $_GET['name'] : null;
    
    if ($params['sqlname']) {
        if ($params['is'] === 1) {
            $createSql = create_sql($mysql, $params);
            if ($createSql === 1) {
                $result['status'] = 1;
                $result['data'] = true;
                $result['message'] = '创建数据库 (' . $params["sqlname"] .') 成功';
            }
            else {
                $result['status'] = 0;
                $result['data'] = false;
                $result['message'] = '创建数据库 (' . $params["sqlname"] .') 失败';
            };
        }
        else {
            $createSql = drop_sql($mysql, $params);
            if ($createSql === 1) {
                $result['status'] = 1;
                $result['data'] = true;
                $result['message'] = '删除数据库 (' . $params["sqlname"] .') 成功';
            }
            else {
                $result['status'] = 0;
                $result['data'] = false;
                $result['message'] = '删除数据库 (' . $params["sqlname"] .') 失败';
            };
        };
    }
    else {
        $result['status'] = 0;
        $result['data'] = false;
        $result['message'] = '数据库名称为空';
    };

    // 关闭连接
    $mysql->close();
}
else {
    $result['message'] = '数据库连接错误';
};

echo json_encode($result);
?>